const {
	db,
	dbCmd,
	result_success,
	result_error,
	bluetoothCollection
} = require('../../common/constants')
module.exports = async function(params = {}) {
	let open_id = params.open_id
	if (!open_id) {
		let appid = 'wx5d64564101862c91'
		let secret = '7d4c42743241d1517e3d97257ba00c54'
		let authCode = params.authCode
		let url = "https://api.weixin.qq.com/sns/jscode2session?"
		url = url + "appid=" + appid
		url = url + "&secret=" + secret
		url = url + "&js_code=" + authCode
		url = url + "&grant_type='authorization_code'"
		let res = await uniCloud.httpclient.request(url, {
			method: 'GET',
			contentType: 'json', // 指定以application/json发送data内的数据
			dataType: 'json' // 指定返回值为json格式，自动进行parse
		})
		if (res.status === 200 && !res.data.errcode) {
			open_id = res.data.openid
		}
		if (!open_id) {
			return result_error('获取失败,请重试')
		}
	}
	let bluetooth = await bluetoothCollection.where({
		open_id: open_id
	}).get()
	bluetooth = bluetooth.data
	return result_success({
		data: bluetooth,
		open_id: open_id
	})
}